/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.view;

import Utils.MyOwnFocusTraversalPolicy;
import Utils.TextFieldFilter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.ActionMap;
import javax.swing.InputMap;

/**
 *
 * @author frankowp
 */
public class DowSubPanel extends ContentPanel {

    //private MyLabel acregLab;
    private MyOwnFocusTraversalPolicy travelsalPolicy;
    private MyLabel bwLab;
    private MyLabel biLab;
    private MyLabel crewLab;
    private MyLabel pantryLab;
    private MyLabel pantryFwdLab;
    private MyLabel pantryMidLab;
    private MyLabel pantryAftLab;
    private MyLabel configLab;
    //private MyTextField acregText;
    private MyTextField bwText;
    private MyTextField biText;
    private MyTextField pantryFwdText;
    private MyTextField pantryMidText;
    private MyTextField pantryAftText;
    private MyComboBox crewCombo;
    private MyComboBox configCombo;
    private MySeparator sep1;
    private MySeparator sep2;
    private MyLabel opLimits;
    private MyLabel mtowLab;
    private MyLabel mlawLab;
    private MyTextField mtowText;
    private MyTextField mlawText;
    private MyLabel additional;
    private MyLabel adj1Lab;
    private MyLabel adj2Lab;
    private MyLabel adj3Lab;
    private MyLabel adjLab;
    private MyLabel adjWeightLab;
    private MyLabel adjIndexLab;
    private MyLabel adjDescriptionLab;
    private MyTextField adj1WeightText;
    private MyTextField adj1IndexText;
    private MyTextField adj1DescriptionText;
    private MyTextField adj2WeightText;
    private MyTextField adj2IndexText;
    private MyTextField adj2DescriptionText;
    private MyTextField adj3WeightText;
    private MyTextField adj3IndexText;
    private MyTextField adj3DescriptionText;

    public DowSubPanel() {
        super();
        initPanel();
    }

    private void initPanel() {
        //AC REG, BW, BI, CREW, reszta ok.
        //acregLab = new MyLabel("AC REG.", MyLabel.BOLD);
        bwLab = new MyLabel("BASIC WEIGHT", MyLabel.BOLD);
        biLab = new MyLabel("BASIC INDEX", MyLabel.BOLD);
        crewLab = new MyLabel("CREW", MyLabel.BOLD);
        //crewLab.setSize(200, crewLab.getSize().height);
        crewLab.setPreferredSize(new Dimension(crewLab.getPreferredSize().width + 15, crewLab.getPreferredSize().height));
        pantryLab = new MyLabel("PANTRY", MyLabel.BOLD);
        pantryLab.setSize(200, pantryLab.getSize().height);
        pantryLab.setPreferredSize(new Dimension(80, (int) pantryLab.getPreferredSize().getHeight()));
        configLab = new MyLabel("CONFIG", MyLabel.BOLD);
        configLab.setSize(200, configLab.getSize().height);
        configLab.setPreferredSize(new Dimension(80, (int) configLab.getPreferredSize().getHeight()));
        pantryFwdLab = new MyLabel("FWD");
        pantryFwdLab.setFont(12);
        pantryMidLab = new MyLabel("MID");
        pantryMidLab.setFont(12);
        pantryAftLab = new MyLabel("AFT");
        pantryAftLab.setFont(12);
        pantryAftLab.setPreferredSize(new Dimension(pantryAftLab.getPreferredSize().width + 10, pantryAftLab.getPreferredSize().height));


        pantryLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);

        bwText = new MyTextField(8);
        bwText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 5, false));
        bwText.requestFocus(true);

        biText = new MyTextField(8);
        biText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 2, false));

        crewCombo = new MyComboBox();
        crewCombo.setPreferredSize(new Dimension(crewCombo.getPreferredSize().width + 10, crewCombo.getPreferredSize().height));
        crewCombo.setFocusable(true);

        configCombo = new MyComboBox();
        configCombo.setPreferredSize(new Dimension(configCombo.getPreferredSize().width + 10, configCombo.getPreferredSize().height));
        configCombo.setFocusable(true);
        configCombo.setScrollBar();

        pantryFwdText = new MyTextField(4);
        pantryFwdText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 4, false));

        pantryMidText = new MyTextField(4);
        pantryMidText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 4, false));

        pantryAftText = new MyTextField(4);
        pantryAftText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 4, false));



        opLimits = new MyLabel("OPERATING LIMITS");
        opLimits.setFont(12);

        mtowLab = new MyLabel("MTOW", MyLabel.BOLD);
        mtowText = new MyTextField(8);
        mtowText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 10, true));

        mlawLab = new MyLabel("MLAW", MyLabel.BOLD);
        mlawText = new MyTextField(8);
        mlawText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 10, true));

        additional = new MyLabel("ADDITIONAL ITEMS", MyLabel.BOLD);
        additional.setFont(12);

        adj1Lab = new MyLabel("ADJ1", MyLabel.BOLD);
        adj2Lab = new MyLabel("ADJ2", MyLabel.BOLD);
        adj3Lab = new MyLabel("ADJ3", MyLabel.BOLD);

        adj1WeightText = new MyTextField(4);
        adj1WeightText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC + "-", 6, false));

        adj1IndexText = new MyTextField(4);
        adj1IndexText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC + "-", 3, false));

        adj1DescriptionText = new MyTextField(6);
        adj1DescriptionText.setFieldFilter(new TextFieldFilter(TextFieldFilter.ALPHA_NUMERIC, 10, true));

        adj2WeightText = new MyTextField(4);
        adj2WeightText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC + "-", 6, false));

        adj2IndexText = new MyTextField(4);
        adj2IndexText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC + "-", 3, false));

        adj2DescriptionText = new MyTextField(6);
        adj2DescriptionText.setFieldFilter(new TextFieldFilter(TextFieldFilter.ALPHA_NUMERIC, 10, true));

        adj3WeightText = new MyTextField(4);
        adj3WeightText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC + "-", 6, false));

        adj3IndexText = new MyTextField(4);
        adj3IndexText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC + "-", 3, false));

        adj3DescriptionText = new MyTextField(6);
        adj3DescriptionText.setFieldFilter(new TextFieldFilter(TextFieldFilter.ALPHA_NUMERIC, 10, true));

        adjWeightLab = new MyLabel("weight");
        adjWeightLab.setFont(12);
        adjWeightLab.setPreferredSize(new Dimension(adjWeightLab.getPreferredSize().width + 5, adjWeightLab.getPreferredSize().height));
        adjIndexLab = new MyLabel("index");
        adjIndexLab.setFont(12);
        adjIndexLab.setPreferredSize(new Dimension(adjIndexLab.getPreferredSize().width + 5, adjIndexLab.getPreferredSize().height));

        adjDescriptionLab = new MyLabel("description");
        adjDescriptionLab.setFont(12);

        sep1 = new MySeparator();
        sep2 = new MySeparator();
        add(bwLab);
        add(biLab);
        add(bwText);
        add(biText);


        setLayout(null);
        add(crewLab);
        add(pantryLab);
        add(configLab);
        add(pantryFwdLab);
        add(pantryMidLab);
        add(pantryAftLab);
        add(adjWeightLab);
        add(adjIndexLab);
        add(adjDescriptionLab);

        add(crewCombo);
        add(configCombo);
        add(pantryFwdText);
        add(pantryMidText);
        add(pantryAftText);
        add(sep1);
        add(sep2);
        add(opLimits);
        add(mtowLab);
        add(mtowText);
        add(mlawLab);
        add(mlawText);
        add(additional);
        add(adj1Lab);
        add(adj2Lab);
        add(adj3Lab);
        add(adj1WeightText);
        add(adj1IndexText);
        add(adj1DescriptionText);
        add(adj2WeightText);
        add(adj2IndexText);
        add(adj2DescriptionText);
        add(adj3WeightText);
        add(adj3IndexText);
        add(adj3DescriptionText);


        sep1.setPosition(new Point(289, 1));
        sep1.setOrientation(1);
        sep1.setSize(1, 289);

        bwLab.setPositionByRight(new Point(125, 5));
        bwText.setPosition(new Point(130, 5));
        biLab.setPositionByRight(new Point(125, 40));
        biText.setPosition(new Point(130, 40));
        crewLab.setPositionByRight(new Point(130, 75));
        crewCombo.setPosition(new Point(130, 75));
        crewCombo.setSize(bwText.getSize().width, crewCombo.getSize().height);
        configLab.setPositionByRight(new Point(130, 110));
        configCombo.setPosition(new Point(130, 110));
        configCombo.setSize(bwText.getSize().width, configCombo.getSize().height);
        pantryLab.setPositionByRight(new Point(130, 145));
        pantryFwdText.setPosition(new Point(130, 145));
        pantryMidText.setPosition(new Point(130, 175));
        pantryAftText.setPosition(new Point(130, 205));

        pantryFwdLab.setPositionByRight(new Point(220, 145));
        pantryMidLab.setPositionByRight(new Point(220, 175));
        pantryAftLab.setPositionByRight(new Point(220, 205));

        opLimits.setPosition(new Point(300, 5));
        mtowLab.setPositionByRight(new Point(455, 30));
        mtowText.setPosition(new Point(460, 30));
        mlawLab.setPositionByRight(new Point(455, 60));
        mlawText.setPosition(new Point(460, 60));

        sep2.setPosition(new Point(289, 100));
        sep2.setSize(289, 1);

        additional.setPosition(new Point(300, 105));
        adj1Lab.setPositionByRight(new Point(340, 155));
        adj2Lab.setPositionByRight(new Point(340, 185));
        adj3Lab.setPositionByRight(new Point(340, 215));

        adjWeightLab.setPositionByRight(new Point(400, 130));
        adjIndexLab.setPositionByRight(new Point(460, 130));
        adjDescriptionLab.setPositionByRight(new Point(560, 130));

        adj1WeightText.setPosition(new Point(355, 155));
        adj1IndexText.setPosition(new Point(420, 155));
        adj1DescriptionText.setPosition(new Point(485, 155));

        adj2WeightText.setPosition(new Point(355, 185));
        adj2IndexText.setPosition(new Point(420, 185));
        adj2DescriptionText.setPosition(new Point(485, 185));

        adj3WeightText.setPosition(new Point(355, 215));
        adj3IndexText.setPosition(new Point(420, 215));
        adj3DescriptionText.setPosition(new Point(485, 215));
        setOrderList();
    }

    public void setOrderList() {
        Vector<Component> focusOrder = new Vector<Component>();
        focusOrder.add(bwText);
        focusOrder.add(biText);
        focusOrder.add(crewCombo);
        focusOrder.add(configCombo);
        if(pantryFwdText.isVisible()){
            focusOrder.add(pantryFwdText);
        }
        if(pantryMidText.isVisible()){
            focusOrder.add(pantryMidText);
        }
        if(pantryAftText.isVisible()){
            focusOrder.add(pantryAftText);
        }
        focusOrder.add(mtowText);
        focusOrder.add(mlawText);
        focusOrder.add(adj1WeightText);
        focusOrder.add(adj1IndexText);
        focusOrder.add(adj1DescriptionText);
        focusOrder.add(adj2WeightText);
        focusOrder.add(adj2IndexText);
        focusOrder.add(adj2DescriptionText);
        focusOrder.add(adj3WeightText);
        focusOrder.add(adj3IndexText);
        focusOrder.add(adj3DescriptionText);
        travelsalPolicy = new MyOwnFocusTraversalPolicy(focusOrder);
        this.setFocusTraversalPolicy(travelsalPolicy);
        this.setFocusTraversalPolicyProvider(true);
        this.setFocusCycleRoot(true);
    }

    public void setBwTextFocusListener(FocusListener listener) {
        bwText.addFocusListener(listener);
    }

    public void setBiTextFocusListener(FocusListener listener) {
        biText.addFocusListener(listener);
    }

    void setAcregKeyListener(KeyListener listener) {
        //acregText.addKeyListener(listener);
    }

    public void setPantryFwdTextFocusListener(FocusListener listener) {
        pantryFwdText.addFocusListener(listener);
    }

    public void setPantryMidTextFocusListener(FocusListener listener) {
        pantryMidText.addFocusListener(listener);
    }

    public void setPantryAftTextFocusListener(FocusListener listener) {
        pantryAftText.addFocusListener(listener);
    }

    void setCrewComboActionListener(ActionListener listener) {
        crewCombo.addActionListener(listener);
    }

    public void reset() {
        bwText.setText("");
        biText.setText("");
        pantryAftText.setText("");
        pantryAftText.setVisible(true);
        pantryAftLab.setVisible(true);
        pantryMidText.setText("");
        pantryMidText.setVisible(true);
        pantryMidLab.setVisible(true);
        pantryFwdText.setText("");
        pantryFwdText.setVisible(true);
        pantryFwdLab.setVisible(true);

        pantryFwdText.setPosition(new Point(130, 145));
        pantryMidText.setPosition(new Point(130, 175));
        pantryAftText.setPosition(new Point(130, 205));

        pantryFwdLab.setPositionByRight(new Point(220, 145));
        pantryMidLab.setPositionByRight(new Point(220, 175));
        pantryAftLab.setPositionByRight(new Point(220, 205));

        adj1DescriptionText.setText("");
        adj1IndexText.setText("");
        adj1WeightText.setText("");
        adj2DescriptionText.setText("");
        adj2IndexText.setText("");
        adj2WeightText.setText("");
        adj3DescriptionText.setText("");
        adj3IndexText.setText("");
        adj3WeightText.setText("");
        configCombo.removeAllItems();
        crewCombo.removeAllItems();
        setOrderList();
    }

    public void setAcRegText(String value) {
        //acregText.setText(value);
    }

    public void addCrewComboBoxItem(String item) {
        crewCombo.addItem(item);
    }

    public void addConfigComboBoxItem(String item) {
        configCombo.addItem(item);
        configCombo.setScrollBar();
    }

    public String get_vbasicweight() {
        return bwText.getText();
    }

    public String get_cbasicindex() {
        return biText.getText();
    }

    public String get_vcrew() {
        return (String) crewCombo.getSelectedItem();
    }

    public String get_vpantryfwd() {
        return pantryFwdText.getText();
    }

    public String get_vpantrymid() {
        return pantryMidText.getText();
    }

    public String get_vpantryaft() {
        return pantryAftText.getText();
    }

    public String get_vconfig() {
        return (String) configCombo.getSelectedItem();
    }

    public String get_vadjweight() {
        return adj1WeightText.getText();
    }

    public String get_vadjindex() {
        return adj1IndexText.getText();
    }

    public String get_vadjdescription() {
        return adj1DescriptionText.getText();
    }

    void hidePantryAft() {
        pantryAftLab.setVisible(false);
        pantryAftText.setVisible(false);
        setOrderList();
    }

    void hidePantryMid() {
        pantryMidLab.setVisible(false);
        pantryMidText.setVisible(false);
        setOrderList();
    }

    void hidePantryFwd() {
        pantryFwdLab.setVisible(false);
        pantryFwdText.setVisible(false);
        setOrderList();
    }

    void shiftPantry(String pan, int y) {
        if (pan.equals("fwd")) {
            pantryFwdText.setPosition(new Point(130, y));
            pantryFwdLab.setPositionByRight(new Point(220, y));
        }
        if (pan.equals("mid")){
            pantryMidText.setPosition(new Point(130, y));
            pantryMidLab.setPositionByRight(new Point(220, y));
        }
        if (pan.equals("aft")){
            pantryAftText.setPosition(new Point(130, y));
            pantryAftLab.setPositionByRight(new Point(220, y));
        }
    }

    public void setFirstFieldActive() {
        //acregText.requestFocusInWindow();
    }

    void setBwTextFocused() {
        bwText.requestFocus();
    }

    public String getMlaw() {
        return mlawText.getText();
    }

    public String getMtow() {
        return mtowText.getText();
    }

    public void setMlaw(String value) {
        mlawText.setText(value);
    }

    public void setMtow(String value) {
        mtowText.setText(value);
    }

    void setMtowTextFocList(FocusListener listener) {
        mtowText.addFocusListener(listener);
    }

    void setMlawTextFocList(FocusListener listener) {
        mlawText.addFocusListener(listener);
    }

    public void seTvadj1weight(String value) {
        adj1WeightText.setText(value);
    }

    public void seTvadj1index(String value) {
        adj1IndexText.setText(value);
    }

    public void seTvadj1description(String value) {
        adj1DescriptionText.setText(value);
    }

    public void seTvadj2weight(String value) {
        adj2WeightText.setText(value);
    }

    public void seTvadj2index(String value) {
        adj2IndexText.setText(value);
    }

    public void seTvadj2description(String value) {
        adj2DescriptionText.setText(value);
    }

    public void seTvadj3weight(String value) {
        adj3WeightText.setText(value);
    }

    public void seTvadj3index(String value) {
        adj3IndexText.setText(value);
    }

    public void seTvadj3description(String value) {
        adj3DescriptionText.setText(value);
    }

    public String geTvadj1weight() {
        return adj1WeightText.getText().equals("") ? "0" : adj1WeightText.getText();
    }

    public String geTvadj1index() {
        return adj1IndexText.getText().equals("") ? "0" : adj1IndexText.getText();
    }

    public String geTvadj1description() {
        return adj1DescriptionText.getText().equals("") ? "0" : adj1DescriptionText.getText();
    }

    public String geTvadj2weight() {
        return adj2WeightText.getText().equals("") ? "0" : adj2WeightText.getText();
    }

    public String geTvadj2index() {
        return adj2IndexText.getText().equals("") ? "0" : adj2IndexText.getText();
    }

    public String geTvadj2description() {
        return adj2DescriptionText.getText().equals("") ? "0" : adj2DescriptionText.getText();
    }

    public String geTvadj3weight() {
        return adj3WeightText.getText().equals("") ? "0" : adj3WeightText.getText();
    }

    public String geTvadj3index() {
        return adj3IndexText.getText().equals("") ? "0" : adj3IndexText.getText();
    }

    public String geTvadj3description() {
        return adj3DescriptionText.getText().equals("") ? "0" : adj3DescriptionText.getText();
    }

    public void setConfigListener(ItemListener itemListener){
        configCombo.addItemListener(itemListener);
    }

    public Object getConfigItem(){
        return configCombo.getSelectedItem();
    }

    public void setAdjTextFocusListener(FocusListener fl){
        adj1WeightText.addFocusListener(fl);
        adj1IndexText.addFocusListener(fl);
        adj2WeightText.addFocusListener(fl);
        adj2IndexText.addFocusListener(fl);
        adj3WeightText.addFocusListener(fl);
        adj3IndexText.addFocusListener(fl);
    }


}
